﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmsuamuonsach : Form
    {
        public frmsuamuonsach()
        {
            InitializeComponent();
        }

        private void cboISBN_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboISBN.Text != "")
                {
                    SqlCommand lenh = new SqlCommand("select ISBN,tuasach,ma_nxb,namxb,sotrang,ma_nn from dau_sach", Connection.sqlConnection);
                    SqlDataReader sqlDataReader = lenh.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        if (cboISBN.Text == sqlDataReader.GetString(0))
                        {
                            txttuasach.Text = sqlDataReader.GetString(1);
                            txtnxb.Text = sqlDataReader.GetString(2);
                            txtnamxb.Text = sqlDataReader.GetInt32(3).ToString();
                            txtsotrang.Text = sqlDataReader.GetInt32(4).ToString();
                            txtngonngu.Text = sqlDataReader.GetString(5);
                        }
                    }
                    sqlDataReader.Dispose();
                    //load du lieu len cbosttcuon
                    DataTable bangsttcuon = new DataTable();
                    SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter("select stt_cuon from cuon_sach where isbn='" + cboISBN.Text + "'", Connection.sqlConnection);
                    sqlDataAdapter2.Fill(bangsttcuon);
                    cbosttcuon.DataSource = bangsttcuon;
                    cbosttcuon.ValueMember = "stt_cuon";
                    //load hinh len pictureBoxhinhsach
                    lenh = new SqlCommand("select path_anh from dau_sach where isbn='" + cboISBN.Text + "'", Connection.sqlConnection);
                    Image hinh = Image.FromFile(Convert.ToString(lenh.ExecuteScalar()));
                    pictureBox1.Image = hinh;
                }
                else
                {
                    txttuasach.Clear();
                    txtnxb.Clear();
                    txtnamxb.Clear();
                    txtsotrang.Clear();
                    txtngonngu.Clear();
                    Image anh = null;
                    pictureBox1.Image = anh;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("Lỗi: "+ex.Message,"Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void cbosttcuon_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbosttcuon.Text != "")
                {
                    SqlCommand lenh = new SqlCommand("select * from cuon_sach", Connection.sqlConnection);
                    SqlDataReader sqlDataReader = lenh.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        if (cboISBN.Text == sqlDataReader.GetString(0) && cbosttcuon.Text == sqlDataReader.GetInt32(1).ToString())
                        {
                            txtttrangsach.Text = sqlDataReader.GetString(2);
                        }
                    }
                    sqlDataReader.Dispose();
                }
                else
                    txtttrangsach.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cbomathe_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbomathe.Text != "")
                {
                    SqlCommand lenh = new SqlCommand("select mathe, ttrangthe from the_doc_gia", Connection.sqlConnection);
                    SqlDataReader sqlDataReader = lenh.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        if (cbomathe.Text == sqlDataReader.GetString(0))
                            txttrangthe.Text = sqlDataReader.GetString(1);
                    }
                    sqlDataReader.Dispose();
                }
                else
                    txttrangthe.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnchomuon_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand lenh = new SqlCommand("update phieu_muon set mathe='" + cbomathe.Text + "',ngay_gio_muon='" + cadngaymuon.Text + " " + txtgiomuon.Text + "' where stt_phieu=" + Convert.ToInt32(txtstt_phieu.Text), Connection.sqlConnection);
                lenh.ExecuteNonQuery();

                string isbn = cboISBN.Text;
                int stt_cuon = Convert.ToInt32(cbosttcuon.Text);

                lenh = new SqlCommand("update gom_cuon set stt_phieu=" + Convert.ToInt32(txtstt_phieu.Text) + ",isbn='" + cboISBN.Text + "',stt_cuon=" + Convert.ToInt32(cbosttcuon.Text) + ",ngay_het_han='" + cadngayhethan.Text + "'ghi_chu=N'" + txtghichu.Text + "' where stt_phieu=" + Convert.ToInt32(txtstt_phieu.Text) + " and isbn='" + isbn + "' and stt_cuon=" + stt_cuon, Connection.sqlConnection);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btninphieu.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: "+ex.Message);
            }
        }

        private void frmsuamuonsach_Load(object sender, EventArgs e)
        {
            //load du lieu len cbomathe
            DataTable bangmathe = new DataTable();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select mathe,hten_dg from the_doc_gia", Connection.sqlConnection);
            sqlDataAdapter.Fill(bangmathe);
            cbomathe.DataSource = bangmathe;
            cbomathe.ValueMember = "mathe";
            //load du lieu len cboISBN
            DataTable bangISBN = new DataTable();
            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter("select isbn,trangthai from dau_sach", Connection.sqlConnection);
            sqlDataAdapter1.Fill(bangISBN);
            cboISBN.DataSource = bangISBN;
            cboISBN.ValueMember = "ISBN";
        }

        private void btninphieu_Click(object sender, EventArgs e)
        {
            mathe = cbomathe.Text;
            frmphieu_muon phieumuon = new frmphieu_muon();
            phieumuon.ShowDialog();
            btninphieu.Enabled = false;
            
        }

       
    }
}
